package com.hqd.ch03.v35.beans;

import com.hqd.ch03.v35.beans.property.PropertyEditorRegistry;
import com.hqd.ch03.v35.core.MethodParameter;
import com.hqd.ch03.v35.core.convert.TypeDescriptor;
import com.sun.corba.se.impl.io.TypeMismatchException;

import java.lang.reflect.Field;

/**
 * 类型转换接口，通常与{@link PropertyEditorRegistry}连用
 * {@link PropertyEditorRegistry}负责具体转换器的注册，删除
 * TypeConverter负责寻找具体的转换器进行转换
 */
public interface TypeConverter {
    <T> T convertIfNecessary(Object value, Class<T> requiredType) throws TypeMismatchException;

    <T> T convertIfNecessary(Object value, Class<T> requiredType,
                             MethodParameter methodParam) throws TypeMismatchException;

    <T> T convertIfNecessary(Object value, Class<T> requiredType, Field field)
            throws TypeMismatchException;

    default <T> T convertIfNecessary(Object value, Class<T> requiredType,
                                     TypeDescriptor typeDescriptor) throws TypeMismatchException {

        throw new UnsupportedOperationException("TypeDescriptor resolution not supported");
    }

}
